package com.bruce.demo2.patterns.demo2.structural.demo11.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * @PROJECT_NAME: design_patterns
 * @DESCRIPTION: 菜单类
 * @AUTHOR: CNBruceLee
 * @DATE: 2021/7/30 20:42
 */
public class Menu extends MenuComponent {
    private List<MenuComponent> menuComponents;

    public Menu(String name, int level, MenuComponent parentMenu) {
        super(name, level, parentMenu);
        menuComponents = new ArrayList<MenuComponent>();
    }

    @Override
    public void add(MenuComponent menuComponent) {
        menuComponents.add(menuComponent);
    }

    @Override
    public void remove(MenuComponent menuComponent) {
        menuComponents.remove(menuComponent);
    }

    @Override
    public MenuComponent getChild(int i) {
        return menuComponents.get(i);
    }

    @Override
    public void print() {
        for (int i = 0; i < this.level; i++) {
            System.out.print("--");
        }
        System.out.println(this.getName());

        for (MenuComponent menuComponent : menuComponents) {
            menuComponent.print();
        }
    }
}
